package service

import (
	"MyBlogv2/common/HttpCode"
	"MyBlogv2/common/models"
	"MyBlogv2/common/response"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func GetCategoryList(c *gin.Context) {
	var categoryVoList []models.Category
	models.Db.Model(new(models.Category)).Order("created_at desc").Find(&categoryVoList)
	response.ResponseOK(c, categoryVoList)
}

func AddCategory(c *gin.Context) {
	var categoryDto models.CategoryDto
	if err := c.ShouldBindJSON(&categoryDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	categoryDto.Id = 0
	models.Db.Create(&categoryDto)
	response.ResponseOK(c, nil)
}

func GetCategoryList4Admin(c *gin.Context) {
	var categoryVoList []models.CategoryVo
	models.Db.Table("t_category c").
		Select("c.id, c.category_name, c.created_at, c.updated_at, COUNT(a.id) as article_count").
		Joins("left join t_article a on c.id = a.category_id").
		Group("c.id, c.category_name, c.created_at, c.updated_at").
		Order("c.created_at desc").
		Find(&categoryVoList)
	response.ResponseOK(c, categoryVoList)
}

func SearchCategory(c *gin.Context) {
	var searchCategoryDto models.SearchCategoryDto
	if err := c.ShouldBindJSON(&searchCategoryDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	var categoryVoList []models.CategoryVo
	db := models.Db.Table("t_category c").
		Select("c.id, c.category_name, c.created_at, c.updated_at, COUNT(a.id) as article_count").
		Joins("left join t_article a on c.id = a.category_id")
	if searchCategoryDto.CategoryName != "" {
		db.Where("c.category_name like ?", "%"+searchCategoryDto.CategoryName+"%")
	}
	if !time.Time(searchCategoryDto.StartTime).IsZero() {
		db.Where("c.created_at >= ?", searchCategoryDto.StartTime)
	}
	if !time.Time(searchCategoryDto.EndTime).IsZero() {
		db.Where("c.created_at <= ?", searchCategoryDto.EndTime)
	}
	db.Group("c.id, c.category_name, c.created_at, c.updated_at").
		Order("c.created_at desc").
		Find(&categoryVoList)
	response.ResponseOK(c, categoryVoList)
}

func GetCategoryById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var category models.CategoryVo
	models.Db.Model(new(models.CategoryVo)).Where("id = ?", id).Find(&category)
	var articleCount int64
	models.Db.Model(new(models.Article)).Where("category_id = ?", id).Count(&articleCount)
	category.ArticleCount = int(articleCount)
	response.ResponseOK(c, category)
}

func AddCategory4Category(c *gin.Context) {
	var categoryDto models.CategoryDto
	if err := c.ShouldBindJSON(&categoryDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	categoryDto.Id = 0
	models.Db.Save(&categoryDto)
	response.ResponseOK(c, nil)
}

func AddOrUpdateCategory(c *gin.Context) {
	var categoryDto models.CategoryDto
	if err := c.ShouldBindJSON(&categoryDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	if categoryDto.Id == 0 {
		models.Db.Create(&categoryDto)
	} else {
		models.Db.Where("id = ?", categoryDto.Id).Updates(&categoryDto)
	}
	response.ResponseOK(c, nil)
}

func DeleteCategory(c *gin.Context) {
	var ids []int
	if err := c.ShouldBindJSON(&ids); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	var articleCount int64
	models.Db.Model(new(models.Article)).Where("category_id in (?)", ids).Count(&articleCount)
	if articleCount > 0 {
		response.CommonResponse(c, HttpCode.FAILURE, nil, "存在文章引用该分类，无法删除")
		return
	}
	models.Db.Model(new(models.Category)).Where("id in (?)", ids).Delete(new(models.Category))
	response.ResponseOK(c, nil)
}
